package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Package: com.atguigu.lease.web.admin.controller.apartment
 *
 * @Description:
 * @Author xiongkunYang
 * @Create 2025/4/19 23:06
 */
@Tag(name = "房间属性管理")
@RestController
@RequestMapping("/admin/attr")
@RequiredArgsConstructor
public class AttrController {

    private final AttrKeyService attrKeyService;
    private final AttrValueService attrValueService;

    @Operation(summary = "新增或更新属性名称")
    @PostMapping("key/saveOrUpdate")
    public Result<Boolean> saveOrUpdateAttrKey(@RequestBody AttrKey attrKey) {
        return Result.ok(this.attrKeyService.saveOrUpdate(attrKey));
    }

    @Operation(summary = "新增或更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Result<Boolean> saveOrUpdateAttrValue(@RequestBody AttrValue attrValue) {
        return Result.ok(this.attrValueService.saveOrUpdate(attrValue));
    }


    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result<List<AttrKeyVo>> listAttrInfo() {
        return Result.ok(this.attrKeyService.listAttrKeyVo());
    }

    @Operation(summary = "根据id删除属性名称")
    @DeleteMapping("key/deleteById")
    public Result<Boolean> removeAttrKeyById(@RequestParam Long attrKeyId) {
        boolean isRemovedValue = this.attrValueService
                .remove(new LambdaQueryWrapper<AttrValue>().eq(AttrValue::getAttrKeyId, attrKeyId));
        boolean isRemovedKey = this.attrKeyService.removeById(attrKeyId);
        return Result.ok(isRemovedValue && isRemovedKey);
    }

    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("value/deleteById")
    public Result<Boolean> removeAttrValueById(@RequestParam Long id) {
        return Result.ok(this.attrValueService.removeById(id));
    }

}
